package com.superyee.commonlib.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池
 */
public class ExecutorUtils {

    private static final ExecutorUtils ourInstance = new ExecutorUtils();

    private static ExecutorService mThreadPoolExecutor;

    public static ExecutorUtils getInstance() {
        return ourInstance;
    }

    private ExecutorUtils() {
        if (mThreadPoolExecutor == null) {
            //cpu核心数
            int CPU_COUNT = Runtime.getRuntime().availableProcessors();
            //核心线程数
            int CORE_POOL_SIZE = CPU_COUNT + 1;
//            int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
            mThreadPoolExecutor = Executors.newFixedThreadPool(CORE_POOL_SIZE);
        }
    }

    public ExecutorService getThreadPoolExecutor() {
        return mThreadPoolExecutor;
    }

    public void execute(Runnable runnable) {
        if (runnable != null) {
            mThreadPoolExecutor.execute(runnable);
        }
    }

}
